package dryven.request.controller.result;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import dryven.request.controller.ActionException;
import dryven.request.http.Request;
import dryven.request.http.Response;
import dryven.util.StreamCopier;

public class FileResult implements ActionResult {

	private String filename;
	private String mime;
	
	public FileResult(String filename, String mime) {
		super();
		this.filename = filename;
		this.mime = mime;
	}

	@Override
	public ActionResult process(Request req, Response resp) {
		File f = new File(filename);
		resp.setHeader("Content-Type", mime);
		resp.setHeader("Content-Length", String.valueOf(f.length()));
		StreamCopier cpy;
		try {
			cpy = new StreamCopier(new FileInputStream(f), resp.getResponseStream());
			cpy.copy();
			resp.getResponseStream().flush();
		} catch (Exception e) {
			throw new ActionException("Could not read result file", e);
		}
		return null;
	}

}
